<template>
  <div>
    <!--卡片-->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-button v-hasPermi="['admin','sms:homesubject:add']" type="primary" small class="addBtn" style="margin:10px" size="mini" icon="el-icon-plus" @click="addDialogVisible = true">新增
      </el-button>
      <!--表格-->
      <el-table :data="listForm" stripe border style="width: 100%">
        <el-table-column prop="id" header-align="center" align="center" label="id" />
        <el-table-column prop="name" header-align="center" align="center" label="专题名字" />
        <el-table-column prop="title" header-align="center" align="center" label="专题标题" />
        <el-table-column prop="subTitle" header-align="center" align="center" label="专题副标题" />
        <el-table-column prop="status" header-align="center" align="center" label="显示状态" />
        <el-table-column prop="url" header-align="center" align="center" label="详情连接" />
        <el-table-column prop="sort" header-align="center" align="center" label="排序" />
        <el-table-column prop="img" header-align="center" align="center" label="专题图片地址" />
        <!--操作-->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip :enterable="false" effect="dark" content="编辑" placement="top">
              <el-button v-hasPermi="['admin','sms:homesubject:edit']" type="success" icon="el-icon-edit" size="mini" @click="editBtn(scope.row.id)" />
            </el-tooltip>
            <el-tooltip :enterable="false" effect="dark" content="删除" placement="top">
              <el-button v-hasPermi="['admin','sms:homesubject:del']" type="danger" icon="el-icon-delete" size="mini" @click="delBtn(scope.row.id)" />
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!--分页区域-->
      <el-pagination
        background
        :current-page="pageNumber"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />

    </el-card>

    <!--添加对话框-->
    <el-dialog title="添加" :close-on-click-modal="false" :visible.sync="addDialogVisible" width="580px" @close="addHandleClose">
      <span>
        <el-form ref="addFormRef" :model="addForm" :rules="addFormRules" label-width="100px">
          <el-form-item label="id" prop="id">
            <el-input v-model="addForm.id" />
          </el-form-item>
          <el-form-item label="专题名字" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="专题标题" prop="title">
            <el-input v-model="addForm.title" />
          </el-form-item>
          <el-form-item label="专题副标题" prop="subTitle">
            <el-input v-model="addForm.subTitle" />
          </el-form-item>
          <el-form-item label="显示状态" prop="status">
            <el-input v-model="addForm.status" />
          </el-form-item>
          <el-form-item label="详情连接" prop="url">
            <el-input v-model="addForm.url" />
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input v-model="addForm.sort" />
          </el-form-item>
          <el-form-item label="专题图片地址" prop="img">
            <el-input v-model="addForm.img" />
          </el-form-item>
        </el-form>
        <!--底部区域-->
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addClick">确 定</el-button>
      </span>
    </el-dialog>

    <!--编辑对话框-->
    <el-dialog title="编辑" :close-on-click-modal="false" :visible.sync="editDialogVisible" width="580px" @close="editHandleClose">
      <span>
        <el-form ref="editFormRef" :model="editForm" label-width="100px">
          <el-form-item label="id" prop="id">
            <el-input v-model="editForm.id" />
          </el-form-item>
          <el-form-item label="专题名字" prop="name">
            <el-input v-model="editForm.name" />
          </el-form-item>
          <el-form-item label="专题标题" prop="title">
            <el-input v-model="editForm.title" />
          </el-form-item>
          <el-form-item label="专题副标题" prop="subTitle">
            <el-input v-model="editForm.subTitle" />
          </el-form-item>
          <el-form-item label="显示状态" prop="status">
            <el-input v-model="editForm.status" />
          </el-form-item>
          <el-form-item label="详情连接" prop="url">
            <el-input v-model="editForm.url" />
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input v-model="editForm.sort" />
          </el-form-item>
          <el-form-item label="专题图片地址" prop="img">
            <el-input v-model="editForm.img" />
          </el-form-item>
        </el-form>
        <!--底部区域-->
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editClick">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { add, edit, del, getHomeSubject, listHomeSubject } from '@/api/coupon/homesubject'

export default {
  name: 'Subject',
  data() {
    return {
      addDialogVisible: false,
      editDialogVisible: false,
      pageNumber: 1,
      pageSize: 10,
      total: 0,
      listForm: [],
      addForm: {
        id: '',
        name: '',
        title: '',
        subTitle: '',
        status: '',
        url: '',
        sort: '',
        img: ''
      },
      editForm: {},
      addFormRules: {
        id: [
          { required: true, message: '请输入id', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入专题名字', trigger: 'blur' }
        ],
        title: [
          { required: true, message: '请输入专题标题', trigger: 'blur' }
        ],
        subTitle: [
          { required: true, message: '请输入专题副标题', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请输入显示状态', trigger: 'blur' }
        ],
        url: [
          { required: true, message: '请输入详情连接', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入排序', trigger: 'blur' }
        ],
        img: [
          { required: true, message: '请输入专题图片地址', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 初始化列表
    this.initListHomeSubject()
  },

  methods: {
    // 添加按钮
    addBtn() {
      this.addDialogVisible = true
    },

    // 确认添加
    addClick() {
      this.$refs.addFormRef.validate(valid => {
        if (!valid) return
        add(this.addForm).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.initListHomeSubject()
          }
          // 关闭对话框
          this.addDialogVisible = false
        })
      })
    },

    // 编辑按钮
    editBtn(id) {
      this.editDialogVisible = true
      getHomeSubject(id).then(res => {
        if (res.code === 20000) {
          this.editForm = res.data
        }
      })
    },

    // 确认编辑
    editClick() {
      this.$refs.editFormRef.validate(valid => {
        if (!valid) return
        edit(this.editForm).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.initListHomeSubject()
          }
          // 关闭对话框
          this.editDialogVisible = false
        })
      })
    },

    // 删除按钮
    delBtn(id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认操作
        del(id).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.initListHomeSubject()
          }
        })
      })
    },

    // 监听当前页的事件
    handleCurrentChange(pageIndex) {
      this.pageNumber = pageIndex
      this.initListHomeSubject()
    },

    // 监听当前页的条数事件
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.initListHomeSubject()
    },

    // 监听添加对话框的关闭事件，重置表单内容
    addHandleClose() {
      this.$refs.addFormRef.resetFields()
    },

    // 监听编辑对话框的关闭事件,重置表单内容
    editHandleClose() {
      this.$refs.editFormRef.resetFields()
    },

    // 初始化数据
    initListHomeSubject() {
      const params = {
        pageNum: this.pageNumber,
        pageSize: this.pageSize
      }
      listHomeSubject(params).then(res => {
        this.listForm = res.data.records
        this.total = res.data.total
        this.pageNum = res.data.current
      })
    }
  }
}
</script>

<style scoped>
</style>
